package com.link2room.optimus.controller.manager;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.optimus.dto.param.base.PriceStaticBaseParamDto;
import com.link2room.optimus.dto.result.base.PriceDto;
import com.link2room.optimus.dto.result.base.PriceStaticDayDto;
import com.link2room.optimus.facade.IStaticPriceFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.link2room.optimus.constant.PermissionConstant.*;

/**
 * Created by sss on 2016/8/29.
 */
@Api(tags = "基价管理")
@RestController
@RequestMapping("/manager/StaticPrices")
public class StaticPriceManagerController {
    @Resource
    private IStaticPriceFacadeService staticPriceFacadeService;

    @ApiOperation(value = "根据基价代码获取基价",response = PriceDto.class)
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = staticPrices,name ="基价列表")
    public BaseResultDto basePriceStatics(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode,
            @RequestParam(value = "priceCode" ,required = true) String priceCode,
            @RequestParam(value = "rmtype" ,required = false) String rmtype,
            @RequestParam(value = "startDatetime" ,required = false) String startDatetime,
            @RequestParam(value = "endDatetime" ,required = false) String endDatetime
    ) throws Exception {
        PriceStaticBaseParamDto priceStaticBaseParamDto =new PriceStaticBaseParamDto();
        priceStaticBaseParamDto.setLhotelCode(lhotelCode);
        priceStaticBaseParamDto.setStartDatetime(startDatetime);
        priceStaticBaseParamDto.setEndDatetime(endDatetime);
        priceStaticBaseParamDto.setPriceCode(priceCode);
        priceStaticBaseParamDto.setRmtype(rmtype);
        List<PriceDto> priceList = staticPriceFacadeService.listPriceStaticDto(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setListToQueryResult(priceList);
        return l2rRs;
    }

    @ApiOperation(value = "根据基价代码获取每日基价",response = PriceDto.class)
    @RequestMapping(value = "/DayStaticPrices",method = RequestMethod.GET)
    @L2RPermissionResources(value = staticDayPrices,name ="根据基价代码获取每日基价")
    public BaseResultDto dayPriceStatics(
            @RequestParam(value = "lhotelCode") String lhotelCode,
            @RequestParam(value = "priceCode") String priceCode,
            @RequestParam(value = "rmtype") String rmtype,
            @RequestParam(value = "startDatetime" ) String startDatetime,
            @RequestParam(value = "endDatetime") String endDatetime
    ) throws Exception {
        PriceStaticBaseParamDto priceStaticBaseParamDto =new PriceStaticBaseParamDto();
        priceStaticBaseParamDto.setLhotelCode(lhotelCode);
        priceStaticBaseParamDto.setStartDatetime(startDatetime);
        priceStaticBaseParamDto.setEndDatetime(endDatetime);
        priceStaticBaseParamDto.setPriceCode(priceCode);
        priceStaticBaseParamDto.setRmtype(rmtype);
        QueryResult<PriceStaticDayDto> priceList = staticPriceFacadeService.listPriceStaticDays(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(priceList);
        return l2rRs;
    }


    @ApiOperation(value = "保存基价",response = PriceDto.class)
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = newBasePriceStatics,name="新建基价")
    public BaseResultDto newBasePriceStatics(
            @RequestBody PriceStaticBaseParamDto priceStaticBaseParamDto
    ) throws Exception {
        PriceDto priceDto=staticPriceFacadeService.saveOrUpdateStaticPrice(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(priceDto);
        l2rRs.setResultMessage(BaseConstant.BaseMessageConstant.SAVESUCCESS);
        return l2rRs;
    }

    @ApiOperation(value = "保存每日基价列表",response = PriceDto.class)
    @RequestMapping(value = "/DayStaticPrices",method = RequestMethod.POST)
    public BaseResultDto newDayPriceStatics(
            @RequestBody PriceStaticBaseParamDto priceStaticBaseParamDto
    ) throws Exception {
        List<PriceStaticDayDto> priceStaticDayDtoList = staticPriceFacadeService.saveOrUpdateStaticPriceDayList(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setListToQueryResult(priceStaticDayDtoList);
        l2rRs.setResultMessage(BaseConstant.BaseMessageConstant.SAVESUCCESS);
        return l2rRs;
    }

    @ApiOperation(value = "保存每日基价",response = PriceDto.class)
    @RequestMapping(value = "/DayStaticPrices/DayStaticPrice",method = RequestMethod.POST)
    public BaseResultDto newDayPriceStatic(
            @RequestBody PriceStaticBaseParamDto priceStaticBaseParamDto
    ) throws Exception {
        PriceStaticDayDto priceStaticDayDto = staticPriceFacadeService.saveOrUpdateStaticPriceDay(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(priceStaticDayDto);
        l2rRs.setResultMessage(BaseConstant.BaseMessageConstant.SAVESUCCESS);
        return l2rRs;
    }

    @ApiOperation(value = "更新基价",response = PriceDto.class)
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = modifyBasePriceStatics,name="修改基价")
    public BaseResultDto modifyBasePriceStatics(
            @RequestBody PriceStaticBaseParamDto priceStaticBaseParamDto
    ) throws Exception {
        PriceDto priceDto=staticPriceFacadeService.dealStaticPrice(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(priceDto);
        l2rRs.setResultMessage(BaseConstant.BaseMessageConstant.UPDATESUCCESS);
        return l2rRs;
    }

    @ApiOperation(value = "根据ID删除某一天基价")
    @RequestMapping(value = "/DayStaticPrices/{id}",method = RequestMethod.DELETE)
    @L2RPermissionResources(value = deleteBasePriceStatic,name="删除基价")
    public BaseResultDto deleteBasePriceStatics(
            @PathVariable Long id
    ) throws Exception {
        PriceStaticBaseParamDto priceStaticBaseParamDto =new PriceStaticBaseParamDto();
        priceStaticBaseParamDto.setId(id);
        staticPriceFacadeService.deletePriceStaticDay(priceStaticBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultMessage(BaseConstant.BaseMessageConstant.DELETESUCCESS);
        return l2rRs;
    }
}
